#include <cstdio>
#include <algorithm>
#include <cstring>

using namespace std;

const int maxn = 100010;

struct Student {
	int id;  // 证号
	char name[10];  // 姓名
	int grade;  // 成绩
}stu[maxn];

bool cmpById(Student a, Student b) {
	return a.id < b.id;
}

bool cmpByName(Student a, Student b) {
	int result = strcmp(a.name, b.name);
	if (result != 0) {
		return result < 0;
	} else {
		return a.id < b.id;
	}
}

bool cmpByGrade(Student a, Student b) {
	if (a.grade != b.grade) {
		return a.grade < b.grade;
	} else {
		return a.id < b.id;
	}
}

int main() {
	int n, c;
	scanf("%d%d", &n, &c);
	for (int i = 0; i < n; i++) {
		scanf("%d%s%d", &stu[i].id, stu[i].name, &stu[i].grade);
	}

	if (c == 1) {
		sort(stu, stu+n, cmpById);
	} else if (c == 2) {
		sort(stu, stu+n, cmpByName);
	} else if (c == 3) {
		sort(stu, stu+n, cmpByGrade);
	}

	for (int i = 0; i < n; i++) {
		printf("%06d %s %d\n", stu[i].id, stu[i].name, stu[i].grade);
	}

	return 0;
}
